package com.intellagric.controller.facility;

import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.interfaces.facility.FacilityService;
import com.intellagric.pojo.Facility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/admin")
public class FacilityController {
    @Autowired
    private FacilityService facilityService;

    @RequestMapping("/facility/add")
    @ResponseBody
    public ResponseMessage addFacility(Facility facility){
        facility.setId(UUIDUtils.getID());
        facility.setCreateDate(new Date(System.currentTimeMillis()));
        return facilityService.insertFacility(facility);
    }

    @RequestMapping("/facility/edit")
    @ResponseBody
    public ResponseMessage editFacility(Facility facility){
        return facilityService.editFacility(facility);
    }




    @RequestMapping("/facility/delete")
    @ResponseBody
    public ResponseMessage deleteFacility(String id){
        return facilityService.deleteFacility(id);
    }

    @RequestMapping("/facility/batchdelete")
    @ResponseBody
    public LayuiDataGridResult batchDelete(@RequestParam("ids[]") String[] ids){
        return facilityService.batchDeleteFacility(ids);
    }
    @RequestMapping("/facility/batchDelete")
    @ResponseBody
    public LayuiDataGridResult batchDeleteById(@RequestParam("ids[]") String[] ids){
        return facilityService.batchDelete(ids);
    }

    @RequestMapping("/facility/getFacility")
    @ResponseBody
    public Facility getFacility(String facilityId){
        return facilityService.queryFacilityById(facilityId);
    }

    @RequestMapping("/facility/getFacilityList")
    @ResponseBody
    public LayuiDataGridResult getFacilityList(int page, int limit){
        return facilityService.getList(page,limit);
    }

    @RequestMapping("/facility/getFacilityListByCate")
    @ResponseBody
    public LayuiDataGridResult getFacilityListByCate(int page, int limit, String categoryId){
        return facilityService.getListByCategoryId(page,limit,categoryId);
    }
    @RequestMapping("/facility/getFacilityListByCateNotLay")
    @ResponseBody
    public List<Facility> getFacilityListByCateNotLay(int page, int limit, String categoryId){
        return facilityService.getListByCategoryId(page,limit,categoryId).getData();
    }

    @RequestMapping("/facility/search")
    @ResponseBody
    public LayuiDataGridResult search(int page, int limit, String keyword){
        return facilityService.getFacilityByKeyword(page,limit,keyword);
    }
    /**
     * 导入耗材excel表格批量添加数据
     * @return
     */
    @RequestMapping(value = "/facility/addFacilityExcel")
    @ResponseBody
    public LayuiDataGridResult addFacilityExcelData(MultipartFile uploadFile){
        LayuiDataGridResult result=new LayuiDataGridResult();
        String fileName=uploadFile.getOriginalFilename();
        if (Pattern.matches(".*xls.*",fileName.substring(fileName.lastIndexOf(".")+1))){
            return facilityService.addFacilityExcelData(uploadFile);
        }
        result.setMsg("文件格式错误");
        result.setCode(100);
        return result;
    }
}
